package com.elephant.leil.generator.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * 字符串转换工具类
 */
public class StringConvertUtils {

    /**
     * 下划线转驼峰字符串（首字母大写）
     * @param str       输入字符串
     * @return          输出字符串
     */
    public static String underlineToCamelUpperStart(String str) {
        StringBuilder sb = new StringBuilder();
        String[] strArr = str.split("[_]+");
        for (int i = 0; i < strArr.length; i++) {
            String s = strArr[i];
            if (StringUtils.isAllLowerCase(s)) {
                sb.append(StringUtils.capitalize(s));
            } else {
                sb.append(StringUtils.capitalize(StringUtils.lowerCase(s)));
            }
        }
        return sb.toString();
    }


    /**
     * 下划线转驼峰字符串
     * @param str       输入字符串
     * @return          输出字符串
     */
    public static String underlineToCamel(String str) {
        StringBuilder sb = new StringBuilder();
        String[] strArr = str.split("[_]+");
        for (int i = 0; i < strArr.length; i++) {
            String s = strArr[i];
            String lower;
            if (StringUtils.isAllLowerCase(s)) {
                lower = s;
            } else {
                lower = StringUtils.lowerCase(s);
            }
            if (i == 0) {
                sb.append(lower);
            } else {
                sb.append(StringUtils.capitalize(lower));
            }
        }
        return sb.toString();
    }


    /**
     * 下划线转换为中划线字符串
     * @param str       输入字符串
     * @return          输出字符串
     */
    public static String underlineToMiddleLine(String str) {
        String s = StringUtils.lowerCase(str);
        String midLineStr = s.replaceAll("[_]+", "-");
        return midLineStr;
    }

}
